#include "NowPlaying.h"

void NowPlaying::draw(HDC hdc)
{
	Bitmap bmp(width,height);
	Graphics *fromImg = Graphics::FromImage(&bmp);


	
	
	FontFamily  fontFamily(L"Arial");
	Font        font(&fontFamily, 12, FontStyleRegular, UnitPixel);

	SolidBrush  brushTextColor(Color(255, 204, 204, 204));
	SolidBrush  brushBackground(Color(255, 85, 85, 85));
	Pen			penBorder(Color(255, 0, 0, 0));

	

	// nupiesiamas fonas po tekstu
	fromImg->FillRectangle(&brushBackground, 1, 1, width-2, height-2);

	if(titleLength > 0)
	{

		//WCHAR *txt = new WCHAR[260];
		//_stprintf(txt, _T("%s"), title);
		//swprintf(txt, L"%s", title);

		// atvaizduojamas tekstas
		StringFormat* stringFormat = StringFormat::GenericDefault()->Clone();
		//StringFormatFlags(left());
		fromImg->DrawString(songTitle.c_str(), -1, &font, RectF(textStart, 0, width-2-textStart+10, height), stringFormat ,&brushTextColor);
		delete stringFormat;
	}

	// uzdedamas remelis. Dedamas pabaigoje todel kad tekstas kartais ji dengia
	fromImg->DrawRectangle(&penBorder, 0, 0, width-1, height-1);


	
	Graphics    graphics(hdc);
	graphics.DrawImage(&bmp, x, y, width, height);

	delete fromImg;
}


void NowPlaying::update()
{
	if(titleLength > width)
	{
		textStart += direction;
		if(direction == 1)
		{// stumiasi i desine
			if(textStart > 1) direction *= -1; 
		}
		else
		{// stumiasi i kaire
			if(textStart + titleLength < width) direction *= -1; 
		}
	}
	else
	{
		direction = 1;
		textStart = 0;
	}










	RECT* lpRect = new RECT;

	lpRect->top = y;
	lpRect->left = x; 

	lpRect-> bottom = y+height;
	lpRect->right = x+width;


	InvalidateRect(hWnd, lpRect, false);

	delete[] lpRect;
}

void NowPlaying::setName(wstring c)
{

	int charLen = c.length();
	songTitle = c;
	/*for(int i = 0; i < 260; i++)
	{
		title[i] = c[i];
		if(c[i] == '\0') break;
		charLen++;
	}*/

	//title[charLen] = '\0';
	//title[0] = 'a';
	//title[1] = L'\0';

	Bitmap bmp(1,1);
	Graphics *fromImg = Graphics::FromImage(&bmp);
	//fromImg->SetTextRenderingHint( TextRenderingHintAntiAlias);

	StringFormat* stringFormat = StringFormat::GenericDefault()->Clone();
	//stringFormat->SetTrimming(NULL);
	//stringFormat->SetFormatFlags(StringFormatFlagsNoFontFallback);


	Font font(&FontFamily(L"Arial"), 12, FontStyleRegular, UnitPixel);
	RectF Rect(0,0,3000,20);
	RectF Bounds1;
	//fromImg->MeasureString(title, charLen, &font, Rect, &Bounds1);
	fromImg->MeasureString(songTitle.c_str(), charLen, &font, Rect, stringFormat, &Bounds1);


	titleLength = Bounds1.GetRight();
	delete fromImg;
	delete stringFormat;
	//delete[] c;


}